/***********************************************************************
 *            Project: CoreCms
 *        ProjectName: 核心内容管理系统                                
 *                Web: https://www.corecms.net                      
 *             Author: 大灰灰                                          
 *              Email: jianweie@163.com                                
 *         CreateTime: 2024/4/10 星期三 22:28:09
 *        Description: 暂无
 ***********************************************************************/


using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreCms.Net.Configuration;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.Expression;
using CoreCms.Net.Model.FromBody;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Filter;
using CoreCms.Net.Loging;
using CoreCms.Net.IServices;
using CoreCms.Net.Utility.Helper;
using CoreCms.Net.Utility.Extensions;
using CoreCms.Net.Web.Admin.Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using NPOI.HSSF.UserModel;
using SqlSugar;

namespace CoreCms.Net.Web.Admin.Controllers
{
    /// <summary>
    /// 支付宝用户信息
    ///</summary>
    [Description("支付宝用户信息")]
    [Route("api/[controller]/[action]")]
    [ApiController]
    [RequiredErrorForAdmin]
    [Authorize(Permissions.Name)]
    public class CoreCmsAliPayUserInfoController : ControllerBase
    {
        private readonly IWebHostEnvironment _webHostEnvironment;
        private readonly ICoreCmsAliPayUserInfoServices _coreCmsAliPayUserInfoServices;

        /// <summary>
        /// 构造函数
        ///</summary>
        public CoreCmsAliPayUserInfoController(IWebHostEnvironment webHostEnvironment
            , ICoreCmsAliPayUserInfoServices coreCmsAliPayUserInfoServices
            )
        {
            _webHostEnvironment = webHostEnvironment;
            _coreCmsAliPayUserInfoServices = coreCmsAliPayUserInfoServices;
        }

        #region 获取列表============================================================
        // POST: Api/CoreCmsAliPayUserInfo/GetPageList
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取列表")]
        public async Task<AdminUiCallBack> GetPageList()
        {
            var jm = new AdminUiCallBack();
            var pageCurrent = Request.Form["page"].FirstOrDefault().ObjectToInt(1);
            var pageSize = Request.Form["limit"].FirstOrDefault().ObjectToInt(30);
            var where = PredicateBuilder.True<CoreCmsAliPayUserInfo>();
            //获取排序字段
            var orderField = Request.Form["orderField"].FirstOrDefault();

            Expression<Func<CoreCmsAliPayUserInfo, object>> orderEx = orderField switch
            {
                "id" => p => p.id,
                "accessToken" => p => p.accessToken,
                "aliPayUserInfoId" => p => p.aliPayUserInfoId,
                "authStart" => p => p.authStart,
                "expiresIn" => p => p.expiresIn,
                "reExpiresIn" => p => p.reExpiresIn,
                "refreshToken" => p => p.refreshToken,
                "userId" => p => p.userId,
                "unionId" => p => p.unionId,
                "createTime" => p => p.createTime,
                _ => p => p.id
            };

            //设置排序方式
            var orderDirection = Request.Form["orderDirection"].FirstOrDefault();
            var orderBy = orderDirection switch
            {
                "asc" => OrderByType.Asc,
                "desc" => OrderByType.Desc,
                _ => OrderByType.Desc
            };
            //查询筛选

            //序列 int
            var id = Request.Form["id"].FirstOrDefault().ObjectToInt(0);
            if (id > 0)
            {
                where = where.And(p => p.id == id);
            }
            //accessToken nvarchar
            var accessToken = Request.Form["accessToken"].FirstOrDefault();
            if (!string.IsNullOrEmpty(accessToken))
            {
                where = where.And(p => p.accessToken.Contains(accessToken));
            }
            //支付宝用户编号 nvarchar
            var aliPayUserInfoId = Request.Form["aliPayUserInfoId"].FirstOrDefault();
            if (!string.IsNullOrEmpty(aliPayUserInfoId))
            {
                where = where.And(p => p.aliPayUserInfoId.Contains(aliPayUserInfoId));
            }
            //授权开始 nvarchar
            var authStart = Request.Form["authStart"].FirstOrDefault();
            if (!string.IsNullOrEmpty(authStart))
            {
                where = where.And(p => p.authStart.Contains(authStart));
            }
            //截止时间 int
            var expiresIn = Request.Form["expiresIn"].FirstOrDefault().ObjectToInt(0);
            if (expiresIn > 0)
            {
                where = where.And(p => p.expiresIn == expiresIn);
            }
            //刷新令牌时间 int
            var reExpiresIn = Request.Form["reExpiresIn"].FirstOrDefault().ObjectToInt(0);
            if (reExpiresIn > 0)
            {
                where = where.And(p => p.reExpiresIn == reExpiresIn);
            }
            //刷新后token nvarchar
            var refreshToken = Request.Form["refreshToken"].FirstOrDefault();
            if (!string.IsNullOrEmpty(refreshToken))
            {
                where = where.And(p => p.refreshToken.Contains(refreshToken));
            }
            //授权商户的user_id nvarchar
            var userId = Request.Form["userId"].FirstOrDefault();
            if (!string.IsNullOrEmpty(userId))
            {
                where = where.And(p => p.userId.Contains(userId));
            }
            //unionId nvarchar
            var unionId = Request.Form["unionId"].FirstOrDefault();
            if (!string.IsNullOrEmpty(unionId))
            {
                where = where.And(p => p.unionId.Contains(unionId));
            }
            //创建时间 datetime
            var createTime = Request.Form["createTime"].FirstOrDefault();
            if (!string.IsNullOrEmpty(createTime))
            {
                if (createTime.Contains("到"))
                {
                    var dts = createTime.Split("到");
                    var dtStart = dts[0].Trim().ObjectToDate();
                    where = where.And(p => p.createTime > dtStart);
                    var dtEnd = dts[1].Trim().ObjectToDate();
                    where = where.And(p => p.createTime < dtEnd);
                }
                else
                {
                    var dt = createTime.ObjectToDate();
                    where = where.And(p => p.createTime > dt);
                }
            }
            //获取数据
            var list = await _coreCmsAliPayUserInfoServices.QueryPageAsync(where, orderEx, orderBy, pageCurrent, pageSize, true);
            //返回数据
            jm.data = list;
            jm.code = 0;
            jm.count = list.TotalCount;
            jm.msg = "数据调用成功!";
            return jm;
        }
        #endregion

        #region 首页数据============================================================
        // POST: Api/CoreCmsAliPayUserInfo/GetIndex
        /// <summary>
        /// 首页数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("首页数据")]
        public AdminUiCallBack GetIndex()
        {
            //返回数据
            var jm = new AdminUiCallBack { code = 0 };
            return jm;
        }
        #endregion

        #region 编辑数据============================================================
        // POST: Api/CoreCmsAliPayUserInfo/GetEdit
        /// <summary>
        /// 编辑数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("编辑数据")]
        public async Task<AdminUiCallBack> GetEdit([FromBody] FMIntId entity)
        {
            var jm = new AdminUiCallBack();

            var model = await _coreCmsAliPayUserInfoServices.QueryByIdAsync(entity.id, false);
            if (model == null)
            {
                jm.msg = "不存在此信息";
                return jm;
            }
            jm.code = 0;
            jm.data = model;

            return jm;
        }
        #endregion

        #region 编辑提交============================================================
        // POST: Api/CoreCmsAliPayUserInfo/Edit
        /// <summary>
        /// 编辑提交
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("编辑提交")]
        public async Task<AdminUiCallBack> DoEdit([FromBody] CoreCmsAliPayUserInfo entity)
        {
            var jm = await _coreCmsAliPayUserInfoServices.UpdateAsync(entity);
            return jm;
        }
        #endregion

        #region 预览数据============================================================
        // POST: Api/CoreCmsAliPayUserInfo/GetDetails/10
        /// <summary>
        /// 预览数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("预览数据")]
        public async Task<AdminUiCallBack> GetDetails([FromBody] FMIntId entity)
        {
            var jm = new AdminUiCallBack();

            var model = await _coreCmsAliPayUserInfoServices.QueryByIdAsync(entity.id, false);
            if (model == null)
            {
                jm.msg = "不存在此信息";
                return jm;
            }
            jm.code = 0;
            jm.data = model;

            return jm;
        }
        #endregion
    }
}
